package top.lixb.libcommon.utils;

import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.CharacterStyle;
import android.text.style.ForegroundColorSpan;

import java.math.RoundingMode;
import java.text.DecimalFormat;

public class StringUtils {
    public final static String UTF_8 = "utf-8";

    /**
     * 判断字符串是否有值，如果为null或者是空字符串或者只有空格或者为"null"字符串，则返回true，否则则返回false
     */
    public static boolean isEmpty(String value) {
        if (value != null && !"".equalsIgnoreCase(value.trim())
                && !"null".equalsIgnoreCase(value.trim())) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * dddddddddddd
     * dddd****dddd
     * @param str
     * @return
     */
    public static String stringDigitReplace(String str) {
        int          len = str.length();
        StringBuffer sb  = new StringBuffer();
        if (len > 4) {
            for (int i = 0; i < str.length(); i++) {
                char c = str.charAt(i);
                if (i < 4 || i > str.length() - 5) {
                    sb.append(c);
                } else {
                    sb.append('*');
                }
            }
        } else {
            sb.append(str);
        }
        return sb.toString();
    }


    /**
     * 得到颜色
     * @param frontStr
     * @param blackStr
     * @param blackColor
     * @return
     */
    public static String getColorText(String frontStr, String blackStr, String blackColor) {

        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer
                .append(frontStr)
                .append("<font color='")
                .append(blackColor)
                .append("'>")
                .append(blackStr)
                .append("</font>");
//        "默认颜色<font color='#FF0000'>红颜色</font>";
        return stringBuffer.toString();
    }

    /**
     * 得到颜色
     * @param frontStr
     * @param blackStr
     * @param blackColor
     * @return
     */
    public static Spanned getHtmlText(String frontStr, String blackStr, String blackColor) {

        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer
                .append(frontStr)
                .append("<font color='")
                .append(blackColor)
                .append("'>")
                .append(blackStr)
                .append("</font>");
//        "默认颜色<font color='#FF0000'>红颜色</font>";
        return Html.fromHtml(stringBuffer.toString());
    }


    /**
     * 判断多个字符串是否相等，如果其中有一个为空字符串或者null，则返回false，只有全相等才返回true
     */
    public static boolean isEquals(String... agrs) {
        String last = null;
        for (int i = 0; i < agrs.length; i++) {
            String str = agrs[i];
            if (isEmpty(str)) {
                return false;
            }
            if (last != null && !str.equalsIgnoreCase(last)) {
                return false;
            }
            last = str;
        }
        return true;
    }

    /**
     * 返回一个高亮spannable
     *
     * @param content 文本内容
     * @param color   高亮颜色
     * @param start   起始位置
     * @param end     结束位置
     * @return 高亮spannable
     */
    public static CharSequence getHighLightText(String content, int color,
                                                int start, int end) {
        if (TextUtils.isEmpty(content)) {
            return "";
        }
        start = start >= 0 ? start : 0;
        end = end <= content.length() ? end : content.length();
        SpannableString spannable = new SpannableString(content);
        CharacterStyle  span      = new ForegroundColorSpan(color);
        spannable.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannable;
    }


    /**
     * 格式化文件大小，不保留末尾的0
     */
    public static String formatFileSize(long len) {
        return formatFileSize(len, false);
    }

    /**
     * 格式化文件大小，保留末尾的0，达到长度一致
     */
    public static String formatFileSize(long len, boolean keepZero) {
        String        size;
        DecimalFormat formatKeepTwoZero = new DecimalFormat("#.00");
        DecimalFormat formatKeepOneZero = new DecimalFormat("#.0");
        if (len < 1024) {
            size = String.valueOf(len + "B");
        } else if (len < 10 * 1024) {
            // [0, 10KB)，保留两位小数
            size = String.valueOf(len * 100 / 1024 / (float) 100) + "KB";
        } else if (len < 100 * 1024) {
            // [10KB, 100KB)，保留一位小数
            size = String.valueOf(len * 10 / 1024 / (float) 10) + "KB";
        } else if (len < 1024 * 1024) {
            // [100KB, 1MB)，个位四舍五入
            size = String.valueOf(len / 1024) + "KB";
        } else if (len < 10 * 1024 * 1024) {
            // [1MB, 10MB)，保留两位小数
            if (keepZero) {
                size = String.valueOf(formatKeepTwoZero.format(len * 100 / 1024
                        / 1024 / (float) 100))
                        + "MB";
            } else {
                size = String.valueOf(len * 100 / 1024 / 1024 / (float) 100)
                        + "MB";
            }
        } else if (len < 100 * 1024 * 1024) {
            // [10MB, 100MB)，保留一位小数
            if (keepZero) {
                size = String.valueOf(formatKeepOneZero.format(len * 10 / 1024
                        / 1024 / (float) 10))
                        + "MB";
            } else {
                size = String.valueOf(len * 10 / 1024 / 1024 / (float) 10)
                        + "MB";
            }
        } else if (len < 1024 * 1024 * 1024) {
            // [100MB, 1GB)，个位四舍五入
            size = String.valueOf(len / 1024 / 1024) + "MB";
        } else {
            // [1GB, ...)，保留两位小数
            size = String.valueOf(len * 100 / 1024 / 1024 / 1024 / (float) 100)
                    + "GB";
        }
        return size;
    }

    public static DecimalFormat df2 = new DecimalFormat("0.00");
    public static DecimalFormat df4 = new DecimalFormat("0.0000");

    static {
        df2.setRoundingMode(RoundingMode.DOWN);
        df4.setRoundingMode(RoundingMode.DOWN);
    }
//    static DecimalFormat df2 = new DecimalFormat(" ");


    public static String doubleFormat(String num) {
        if (TextUtils.isEmpty(num)) {
            return "0.00";
        } else {
            return df2.format(Double.valueOf(num));
        }
    }

    public static String double4fFormat(String num) {
        if (TextUtils.isEmpty(num)) {
            return "0.0000";
        } else {
            return double4fFormat(Double.valueOf(num));
        }
    }

    public static String double4fFormat(Double num) {

        if (TextUtils.isEmpty(String.valueOf(num))) {
            return "0.0000";
        } else {
            return df4.format(num);
        }
    }



}
